package com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api;

import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;

import java.lang.ref.SoftReference;

import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;

public abstract class BaseApi<T> implements Func1<T, T> {

    /*rx生命周期管理*/
    private SoftReference<RxAppCompatActivity> rxAppCompatActivity;

    /*回调*/
    private SoftReference<HttpOnNextListener> listener;

    /*是否能取消加载框*/
    private boolean cancel;

    /*是否显示加载框*/
    private boolean showProgress;

    /*是否需要缓存处理*/
    private boolean cache;

    /*基础url*/
    private  String baseUrl;

    /*方法-如果需要缓存必须设置这个参数；不需要不用设置*/
    private String mothed;

    /*超时时间-默认6秒*/
    private int connectionTime = 6;

    /*有网情况下的本地缓存时间默认n秒(有网情况下在n秒内不会从网络拉取数据，短时间内从本地取数据)*/
    private int cookieNetWorkTime = 60;

    /*无网络的情况下本地缓存时间默认30天*/
    private int cookieNoNetWorkTime = 24*60*60*30;

    public BaseApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) {
        setListener(listener);
        setRxAppCompatActivity(rxAppCompatActivity);
        setShowProgress(true);
        setCache(true);
    }

    /*设置参数*/
    public abstract Observable getObservable(Retrofit retrofit);

    public void setRxAppCompatActivity(RxAppCompatActivity rxAppCompatActivity) {
        this.rxAppCompatActivity =new SoftReference(rxAppCompatActivity) ;
    }

    /*获取当前rx生命周期*/
    public  RxAppCompatActivity getRxAppCompatActivity() {
        return rxAppCompatActivity.get();
    }

    public SoftReference<HttpOnNextListener> getListener() {
        return listener;
    }

    public void setListener(HttpOnNextListener listener) {
        this.listener = new SoftReference(listener);
    }

    public int getCookieNoNetWorkTime() {
        return cookieNoNetWorkTime;
    }

    public void setCookieNoNetWorkTime(int cookieNoNetWorkTime) {
        this.cookieNoNetWorkTime = cookieNoNetWorkTime;
    }

    public int getCookieNetWorkTime() {
        return cookieNetWorkTime;
    }

    public void setCookieNetWorkTime(int cookieNetWorkTime) {
        this.cookieNetWorkTime = cookieNetWorkTime;
    }

    public String getMothed() {
        return mothed;
    }

    public void setMothed(String mothed) {
        this.mothed = mothed;
    }

    public int getConnectionTime() {
        return connectionTime;
    }

    public void setConnectionTime(int connectionTime) {
        this.connectionTime = connectionTime;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public String getUrl() {
        return baseUrl + mothed;
    }

    public boolean isCache() {
        return cache;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public boolean isShowProgress() {
        return showProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public boolean isCancel() {
         return cancel;
     }

     public void setCancel(boolean cancel) {
         this.cancel = cancel;
     }

    @Override
    public T call(T httpResult) {
        return httpResult;
    }
}
